package com.northone.statistics.fragment;

import java.util.LinkedList;
import java.util.List;

import com.northone.statistics.R;
import com.northone.statistics.activity.MonthDataActivity;
import com.northone.statistics.doman.WeekData;
import com.northone.statistics.utils.ToastUtil;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public abstract class WeekFragment extends Fragment implements OnClickListener {
	protected boolean isShowData;
	protected View view;
	protected LinearLayout input_weekData, show_weekData, ll_week_btn;
	protected TextView weekTitle, tv_normal, tv_baptism, tv_morningRise, tv_pray, tv_gospeler, tv_familyMett,
			tv_rankMeet, tv_sundayMeet;

	protected Button lastWeek, nextWeek, edit, save, clear;
	protected EditText et_normalNum, et_baptismNum, et_morningRise, et_prayNum, et_gospeler, et_familyMett,
			et_rankMeetNum, et_sundayMeetNum;
	protected MonthDataActivity activity;
	protected String normalNum, baptismNum, morningRise, prayNum, gospeler, familyMett, rankMeetNum, sundayMeetNum;

	private LinkedList<WeekData> weekDatas;

	private TextWatcher watcher = new TextWatcher() {
		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			if (TextUtils.isEmpty(et_normalNum.getText().toString())
					&& TextUtils.isEmpty(et_baptismNum.getText().toString())
					&& TextUtils.isEmpty(et_morningRise.getText().toString())
					&& TextUtils.isEmpty(et_prayNum.getText().toString())
					&& TextUtils.isEmpty(et_gospeler.getText().toString())
					&& TextUtils.isEmpty(et_familyMett.getText().toString())
					&& TextUtils.isEmpty(et_rankMeetNum.getText().toString())
					&& TextUtils.isEmpty(et_sundayMeetNum.getText().toString())) {
				ll_week_btn.setVisibility(View.INVISIBLE);
			} else {
				ll_week_btn.setVisibility(View.VISIBLE);
				edit.setVisibility(View.GONE);
			}
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
		}

		@Override
		public void afterTextChanged(Editable s) {
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		activity = (MonthDataActivity) getActivity();
		weekDatas = activity.weekDatas;
	};

	public void initView() {
		// final TextInputLayout normalNum =
		// (TextInputLayout)view.findViewById(R.id.normalNum);
		// normalNum.setHint("请输入本周正常聚会人数");
		weekTitle = (TextView) view.findViewById(R.id.tv_weekTitle);

		input_weekData = (LinearLayout) view.findViewById(R.id.input_weekData);

		et_normalNum = (EditText) view.findViewById(R.id.et_normalNum);
		et_baptismNum = (EditText) view.findViewById(R.id.et_baptismNum);
		et_morningRise = (EditText) view.findViewById(R.id.et_morningRise);
		et_prayNum = (EditText) view.findViewById(R.id.et_prayNum);
		et_gospeler = (EditText) view.findViewById(R.id.et_gospeler);
		et_familyMett = (EditText) view.findViewById(R.id.et_familyMett);
		et_rankMeetNum = (EditText) view.findViewById(R.id.et_rankMeetNum);
		et_sundayMeetNum = (EditText) view.findViewById(R.id.et_sundayMeetNum);

		show_weekData = (LinearLayout) view.findViewById(R.id.show_weekData);

		tv_normal = (TextView) view.findViewById(R.id.tv_normal);
		tv_baptism = (TextView) view.findViewById(R.id.tv_baptism);
		tv_morningRise = (TextView) view.findViewById(R.id.tv_morningRise);
		tv_pray = (TextView) view.findViewById(R.id.tv_pray);
		tv_gospeler = (TextView) view.findViewById(R.id.tv_gospeler);
		tv_familyMett = (TextView) view.findViewById(R.id.tv_familyMett);
		tv_rankMeet = (TextView) view.findViewById(R.id.tv_rankMeet);
		tv_sundayMeet = (TextView) view.findViewById(R.id.tv_sundayMeet);

		ll_week_btn = (LinearLayout) view.findViewById(R.id.ll_week_btn);
		lastWeek = (Button) view.findViewById(R.id.btn_lastWeek);
		nextWeek = (Button) view.findViewById(R.id.btn_nextWeek);

		edit = (Button) view.findViewById(R.id.btn_edit);
		save = (Button) view.findViewById(R.id.btn_save);
		clear = (Button) view.findViewById(R.id.btn_clear);
	}

	public void initData() {
		lastWeek.setOnClickListener(this);
		nextWeek.setOnClickListener(this);
		edit.setOnClickListener(this);
		save.setOnClickListener(this);
		clear.setOnClickListener(this);
		et_normalNum.addTextChangedListener(watcher);
	}

	protected void clearData() {
		et_normalNum.setText("");
		et_baptismNum.setText("");
		et_morningRise.setText("");
		et_prayNum.setText("");
		et_gospeler.setText("");
		et_familyMett.setText("");
		et_rankMeetNum.setText("");
		et_sundayMeetNum.setText("");
		ll_week_btn.setVisibility(View.INVISIBLE);
	}

	/** 保存周数据，返回是否保存成功 */
	protected boolean saveWeekData(WeekData weekData, int savePosition) {
		normalNum = et_normalNum.getText().toString();
		baptismNum = et_baptismNum.getText().toString();
		morningRise = et_morningRise.getText().toString();
		prayNum = et_prayNum.getText().toString();
		gospeler = et_gospeler.getText().toString();
		familyMett = et_familyMett.getText().toString();
		rankMeetNum = et_rankMeetNum.getText().toString();
		sundayMeetNum = et_sundayMeetNum.getText().toString();
		if (TextUtils.isEmpty(normalNum)) {
			ToastUtil.showToast(activity, "请输入正常聚会人数 !");
			return false;
		}
		if (TextUtils.isEmpty(sundayMeetNum)) {
			ToastUtil.showToast(activity, "请输入主日聚会人数 !");
			return false;
		}
		tv_normal.setText("正常聚会" + normalNum + "人");
		if (!TextUtils.isEmpty(baptismNum)) {
			tv_baptism.setText("受浸" + baptismNum + "人");
			tv_baptism.setVisibility(View.VISIBLE);
			try {
				weekData.baptismNum = Integer.parseInt(baptismNum);
			} catch (NumberFormatException e) {
				e.printStackTrace();
				ToastUtil.showToast(activity, "受浸人数格式错误");
			}
		} else {
			tv_baptism.setVisibility(View.GONE);
			weekData.baptismNum = 0;
		}
		if (!TextUtils.isEmpty(morningRise)) {
			tv_morningRise.setText("晨兴" + morningRise + "人");
			tv_morningRise.setVisibility(View.VISIBLE);
			try {
				weekData.morningRiseNum = Integer.parseInt(morningRise);
			} catch (NumberFormatException e) {
				e.printStackTrace();
				ToastUtil.showToast(activity, "晨兴人数格式错误");
			}
		} else {
			tv_morningRise.setVisibility(View.GONE);
			weekData.morningRiseNum = 0;
		}
		if (!TextUtils.isEmpty(prayNum)) {
			tv_pray.setText("参加祷告聚会" + prayNum + "人");
			tv_pray.setVisibility(View.VISIBLE);
			try {
				weekData.prayNum = Integer.parseInt(prayNum);
			} catch (NumberFormatException e) {
				e.printStackTrace();
				ToastUtil.showToast(activity, "参加祷告人数格式错误");
			}
		} else {
			tv_pray.setVisibility(View.GONE);
			weekData.prayNum = 0;
		}
		if (!TextUtils.isEmpty(gospeler)) {
			tv_gospeler.setText("传福音" + gospeler + "人");
			tv_gospeler.setVisibility(View.VISIBLE);
			try {
				weekData.gospelerNum = Integer.parseInt(gospeler);
			} catch (NumberFormatException e) {
				e.printStackTrace();
				ToastUtil.showToast(activity, "传福音人数格式错误");
			}
		} else {
			tv_gospeler.setVisibility(View.GONE);
			weekData.gospelerNum = 0;
		}
		if (!TextUtils.isEmpty(familyMett)) {
			tv_familyMett.setText("家聚会" + familyMett + "次");
			tv_familyMett.setVisibility(View.VISIBLE);
			try {
				weekData.familyMettNum = Integer.parseInt(familyMett);
			} catch (NumberFormatException e) {
				e.printStackTrace();
				ToastUtil.showToast(activity, "家聚会次数格式错误");
			}
		} else {
			tv_familyMett.setVisibility(View.GONE);
			weekData.familyMettNum = 0;
		}
		if (!TextUtils.isEmpty(rankMeetNum)) {
			tv_rankMeet.setText("排聚会" + rankMeetNum + "次");
			tv_rankMeet.setVisibility(View.VISIBLE);
			try {
				weekData.rankMeetNum = Integer.parseInt(rankMeetNum);
			} catch (NumberFormatException e) {
				e.printStackTrace();
				ToastUtil.showToast(activity, "排聚会次数格式错误");
			}
		} else {
			tv_rankMeet.setVisibility(View.GONE);
			weekData.rankMeetNum = 0;
		}
		tv_sundayMeet.setText("参加主日聚会" + sundayMeetNum + "人");

		try {
			weekData.normalNum = Integer.parseInt(normalNum);
			weekData.sundayMeetNum = Integer.parseInt(sundayMeetNum);
		} catch (NumberFormatException e) {
			e.printStackTrace();
			ToastUtil.showToast(activity, "聚会人数格式错误");
		}

		if (weekDatas.contains(weekData)) {
			weekDatas.set(savePosition, weekData);
		} else {
			try {
				weekDatas.add(savePosition, weekData);
			} catch (IndexOutOfBoundsException e) {
				e.printStackTrace();
				ToastUtil.showToast(activity, "请重新输入上一周的数据 !");
				return false;
			}
		}
		input_weekData.setVisibility(View.GONE);
		show_weekData.setVisibility(View.VISIBLE);
		save.setVisibility(View.GONE);
		edit.setVisibility(View.VISIBLE);
		clear.setVisibility(View.GONE);

		return weekDatas.contains(weekData);
	}

	protected void editWeekInfo(WeekData weekData, int savePosition) {
		input_weekData.setVisibility(View.VISIBLE);
		show_weekData.setVisibility(View.GONE);
		et_normalNum.setText(String.valueOf(weekData.normalNum));
		et_baptismNum.setText(String.valueOf(weekData.baptismNum));
		et_morningRise.setText(String.valueOf(weekData.morningRiseNum));
		et_prayNum.setText(String.valueOf(weekData.prayNum));
		et_gospeler.setText(String.valueOf(weekData.gospelerNum));
		et_familyMett.setText(String.valueOf(weekData.familyMettNum));
		et_rankMeetNum.setText(String.valueOf(weekData.rankMeetNum));
		et_sundayMeetNum.setText(String.valueOf(weekData.sundayMeetNum));

		save.setVisibility(View.VISIBLE);
		edit.setVisibility(View.GONE);
		clear.setVisibility(View.VISIBLE);
		isShowData = !isShowData;
		weekDatas.remove(savePosition);
	}

	protected void showWeekData(WeekData weekData) {
		tv_normal.setText("正常聚会" + weekData.normalNum + "人");
		tv_baptism.setText("受浸" + weekData.baptismNum + "人");
		tv_morningRise.setText("晨兴" + weekData.morningRiseNum + "人");
		tv_pray.setText("参加祷告聚会" + weekData.prayNum + "人");
		tv_gospeler.setText("传福音" + weekData.gospelerNum + "人");
		tv_familyMett.setText("家聚会" + weekData.familyMettNum + "次");
		tv_rankMeet.setText("排聚会" + weekData.rankMeetNum + "次");
		tv_sundayMeet.setText("参加主日聚会" + weekData.sundayMeetNum + "人");

		input_weekData.setVisibility(View.GONE);
		show_weekData.setVisibility(View.VISIBLE);

		save.setVisibility(View.VISIBLE);
		edit.setVisibility(View.VISIBLE);
		clear.setVisibility(View.GONE);
	}

	public void setWeekData(int i) {
		List<WeekData> list = activity.weeks;
		if (i < list.size()) {
			WeekData weekData = list.get(i);
			if (weekDatas.contains(weekData)) {
				weekDatas.set(i, weekData);
			} else {
				weekDatas.add(i, weekData);
			}
			showWeekData(weekData);
		}
	}
}
